#include<Windows.h>
#include<iostream>
#include<process.h>
using namespace std;
HANDLE hSem;

DWORD WINAPI Thread1(LPVOID param){
	cout << "Thread1 is waiting..........\n";
	WaitForSingleObject(hSem, INFINITE);
	for(int i = 0; i < 5; i++){
		cout << "A";
		Sleep(500);
	}
	ReleaseSemaphore(hSem, 1, NULL);
	return 0;
}

DWORD WINAPI Thread2(LPVOID param){
	cout << "Thread2 is waiting..........\n";
	WaitForSingleObject(hSem, INFINITE);
	for(int i = 0; i < 5; i++){
		cout << "B";
		Sleep(500);
	}
	return 0;
}
void main(){

	hSem = CreateSemaphore(NULL, 0, 2, "Sem");

	CreateThread(NULL, 0, Thread1, NULL, NULL, NULL);
	HANDLE hThread2 = CreateThread(NULL, 0, Thread2, NULL, NULL, NULL);

	cout << "\n===========================ReleaseSemaphore =====================\n";
	ReleaseSemaphore(hSem, 1, NULL);


	cout << "Main is waiting.............\n";
	WaitForSingleObject(hThread2, INFINITE);


}